<?php
/** [文档控制器]
 * @Author: 976123967@qq.com
 * @Date:   2015-01-16 11:33:22
 * @Last Modified by:   Administrator
 * @Last Modified time: 2015-03-16 10:31:36
 */
class ArticleAction  extends PublicAction{
	public $logic;
	public $categoryService;
	public $FieldService;

	public $getCid;
	public $postCid;

	private $cache;

	/**
	 * [_initialize 初始化]
	 * @return [type] [description]
	 */
	public function _initialize()
	{
		parent::_initialize();

		$this->logic   		   = D('Article','Logic');
		$this->categoryService = D('Category');
		$this->FieldService    = D('Field');

		$this->getCid  		   = I('get.cid');
		$this->postCid         = I('post.category_cid');
		$this->cache           = S('category');

		
	}


	/**
	 * [index 所有文档列表]
	 * @return [type] [description]
	 */
	public function index()
	{
		// 列表，基本信息分配
		$this->base();
		$data = $this->logic->get_all();
		if($data)
		{
			$this->assign('page',$data['page']);
			unset($data['page']);
		}
		$this->assign('data',$data);
		$this->display();
	}

	/**
	 * [add 添加]
	 */
	public function add()
	{

		if(IS_POST)
		{
			if(!$this->logic->add_article())
				$this->error($this->logic->getError());
			$cur = $this->cache[$this->postCid];
			if($cur['cat_type']==4)
				$this->success('文档添加成功',U('Index/copyright'));
			else
				$this->success('文档添加成功',U('Article/index',array('cid'=>$this->postCid)));
			die;
		}
		if(isset($_SESSION['keditor']))
			unset($_SESSION['keditor']);
		// js验证
		$this->assign('validate',$this->FieldService->get_validate());

		$cur  = $this->cache[$this->getCid];

		// 4代表是单页
		if($cur['cat_type']==4 && $data = $this->logic->check_cid($this->getCid))
			$this->redirect('Article/edit',array('cid'=>$cur['cid'],'aid'=>$data['aid']));

		// 添加时，基本信息分配
		$this->base();

		// 读取自定义模型表单信息
		$extForm = $this->FieldService->get_field_form($cur['model_mid']);
		$this->assign('extForm',$extForm);


		$this->display();
	}

	/**
	 * [add 编辑]
	 */
	public function edit()
	{

		if(IS_POST)
		{
			if(!$this->logic->update_article())
				$this->error($this->logic->getError());
			$cur = $this->cache[$this->postCid];
			if($cur['cat_type']==4)
				$this->success('文档修改成功',U('Index/copyright'));
			else
				$this->success('文档修改成功',U('Article/index',array('cid'=>$this->postCid)));
			
			die;	

		}
		if(isset($_SESSION['keditor']))
			unset($_SESSION['keditor']);
		// js验证
		$this->assign('validate',$this->FieldService->get_validate());

		$cur  = $this->cache[$this->getCid];

		// 编辑时，基本信息分配
		$this->base();
		$field = $this->logic->find_one();
		$this->assign('field',$field);
		// 读取图集
		$pics = D('Pic','Logic')->get_all();
		$this->assign('pics',$pics);
		// 读取自定义模型表单信息
		$extForm = $this->FieldService->get_field_form($cur['model_mid'],$field);
		$this->assign('extForm',$extForm);
		
		$this->display();
	}


	/**
	 * [base 添加和编辑基本信息分配]
	 * @return [type] [description]
	 */
	private function base()
	{
		if(!isset($this->cache[$this->getCid]))
			$this->error('链接错误');

		// 当前访问的栏目信息
		$cur  = $this->cache[$this->getCid];

		$this->assign('curCatType',$cur['cat_type']);
		$this->assign('curCname',$cur['cname']);
		$this->assign('curCid',$cur['cid']);

		// 当前访问栏目所有父级栏目cid
		$parentCids  = $this->categoryService->get_parent_cid($this->getCid);
		$parentcid   = $parentCids[count($parentCids)-1];
		$top         = $this->cache[$parentcid];
		$this->assign('topType' ,$top['cat_type']);
		$this->assign('topCname',$top['cname']);

		// 当前栏目顶级栏目的所有子分类
		$topChild    = $this->categoryService->get_child_list($top['cid']);
		$this->assign('topChild',$topChild);

		// 文档属性
		$this->assign('flag',C('flag'));
	}





	/**
	 * [ajax_category 栏目ztree]
	 * @return [type] [description]
	 */
	public function ajax_category()
	{
		$category = $this->categoryService->article_ztree();
		$this -> ajaxReturn($category);
	}

	// 执行批量处理操作
	public function operation()
	{
		if(!$msg = $this->logic->operation())
			$this->error($this->logic->getError());
		$this->success($msg,U('Article/index',array('cid'=>$this->postCid)));
	}


	/**
	 * [ajax_del_pic 删除图片]
	 * @return [type] [description]
	 */
	public function ajax_del_pic()
	{
		if(!IS_AJAX) $this->error('链接错误');

		$picLogic = D('Pic','Logic');
		if(!$picLogic->del_pic())
			$this->ajaxReturn(array('status'=>0,'info'=>$picLogic->getError()));
		$this->ajaxReturn(array('status'=>1,'info'=>'图片删除成功'));
	}

	/**
	 * [ajax_del_attachment 删除附件]
	 * @return [type] [description]
	 */
	public function ajax_del_attachment()
	{
		if(!IS_AJAX) $this->error('链接错误');

		if(!$this->logic->del_attachment())
			$this->ajaxReturn(array('status'=>0,'info'=>$this->logic->getError()));
		$this->ajaxReturn(array('status'=>1,'info'=>'附件删除成功'));
	}

	/**
	 * [del 删除]
	 * @return [type] [description]
	 */
	public function del()
	{
		if(!$this->logic->del_article())
			$this->error($this->logic->getError());
		$this->success('文档删除成功',U('Article/index',array('cid'=>$this->getCid)));
	}


}